<?php 
set_time_limit(180);
$local_file_path = dirname(__FILE__). '/data/';
$remote_file_path = '/home/pcheng/stock/data/';
$table = array( 'house','bank', 'computer', 'steel');
//每周五运行，从SSH取周一至周五的数据
$time = time();
$today = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$daycount = 5;
$sfile = array();
for ($i = 0; $i < $daycount; $i++) {
    $d = $i == 0 ? $today : date('Y-m-d', strtotime("-$i days", strtotime($today)));
    foreach ($table as $t) {
        $sfile[$d][] = $t . '_' . $d . '.php';
    }
}
//从ssh下载文件
$connection = ssh2_connect('mmweb101.dev.la.mezimedia.com', 22);
ssh2_auth_password($connection, 'pcheng', 'Cxc!0378');
foreach ($sfile as $fv) {
    foreach ($fv as $f) {
        ssh2_scp_recv($connection, $remote_file_path . $f, $local_file_path . $f);
    }
}

//写入数据库
foreach ($sfile as $fk=>$fv){
    echo 'http://localhost/cxcstock/file2db.php?date='.$fk;
    file_get_contents('http://localhost/cxcstock/file2db.php?date='.$fk);
    sleep(1);
}